﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using MobileSales.Common.Logger;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using MobileSales.Model.Util;
using MobileSales.Model.Localization;

namespace MobileSales.Model.Params
{
    [DataContract]
    public class Param
    {
        #region Public members
        [DataMember]
        public int Id { get; set; }
        [RegularExpression("^[a-zA-Z][a-zA-Z0-9]*$", ErrorMessage="Only letters and numbers not as first character")]
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
        [DataMember]
        [Display(Name = "Name", ResourceType = typeof(ModelStrings))]
        public string Name { get; set; }
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
        [DataMember]
        [Display(Name = "DisplayName", ResourceType = typeof(ModelStrings))]
        public string DisplayName { get; set; }
        [DataMember]
        [Display(Name = "Description", ResourceType = typeof(ModelStrings))]
        public string Description { get; set; }
        [DataMember]
        [Display(Name = "IsReadOnly", ResourceType = typeof(ModelStrings))]
        public bool IsReadOnly { get; set; }
        [DataMember]
        [Display(Name = "IsRequired", ResourceType = typeof(ModelStrings))]
        public bool IsRequired { get; set; }

        [DataMember(Name = "FieldType")]
        public string _fieldType { get; set; }
        [Display(Name = "FieldType", ResourceType = typeof(ModelStrings))]
        public FieldType FieldType
        {
            get
            {
                if (_fieldType == null) _fieldType = FieldType.Boolean.ToString();
                return (FieldType)Enum.Parse(typeof(FieldType), _fieldType);
            }
            set
            {
                _fieldType = value.ToString();
            }
        }

        [DataMember]
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
        [Display(Name = "DefaultValue", ResourceType = typeof(ModelStrings))]
        public string DefaultValue { get; set; }
        [DataMember]
        [Display(Name = "MaxLength", ResourceType = typeof(ModelStrings))]
        public int? MaxLength { get; set; }

        [DataMember(Name = "ParamType")]
        public string _paramType { get; set; }
        [Display(Name = "ParamType", ResourceType = typeof(ModelStrings))]
        public ParamType ParamType
        {
            get
            {
                if (_paramType == null) _paramType = ParamType.Product.ToString();
                return (ParamType)Enum.Parse(typeof(ParamType), _paramType);
            }
            set
            {
                _paramType = value.ToString();
            }
        } 
        #endregion

        #region GetFieldType

        public Type GetFieldType()
        {
            try
            {
                return Type.GetType(FieldTypes.GetQualifiedName(FieldType), true);
            }
            catch(Exception ex)
            {
                CLogger.LogDebug(ex);
                return null;
            }
        }

        #endregion

        #region GetValue

        public object GetValue(string value)
        {
            if (FieldType == Params.FieldType.String)
            {
                return value;
            }

            Type paramType = this.GetFieldType();
            if (paramType != null)
            {
                MethodInfo parseMethod = null;
                parseMethod = paramType.GetMethod("Parse",new Type[] { typeof(string) });
                if (parseMethod != null)
                {
                    try
                    {
                        return parseMethod.Invoke(null, new object[] { value });
                    }
                    catch (Exception ex)
                    {
                        CLogger.LogDebug(ex);
                        
                    }
                }
            }
            return null;
        }

        #endregion

        #region Constructor
        public Param() { } 
        #endregion
    }
}
